<?php
/* @var $this ZoneController */
/* @var $model Zone */
/* @var horaires array */
/* @var $form CActiveForm */
/* @var $enable_add_more_horaires bool */
?>
<div class="form">

    <?php
    $form = $this->beginWidget('CActiveForm', array(
	  'id' => 'zone-form',
	  'enableClientValidation' => false,
	  'enableAjaxValidation' => false,
    ));
    ?>

    <?php echo $form->errorSummary($model); ?>

    <div class="row">
	  <?php echo $form->labelEx($model, 'modeid'); ?>
	  <?php
	  
	  $currentMode = $model->modeid == 'auto' ? $model->getModeCourant()->id : '';
	  $this->widget('ModeButtons', array(
		'model' => $model,
		'options' => array('currentMode' => $currentMode),
	  ));
	  ?>

	  <?php echo $form->error($model, 'modeid'); ?>
    </div>
    <div class="row">
	  <?php echo $form->labelEx($model, 'jusqua'); ?>
	  <?php
	  echo $form->textField($model, 'jusqua');
	  ?>
	  <?php echo $form->error($model, 'jusqua'); ?>
    </div>
    <?php

    $index = 0;
    foreach (Yii::app()->controller->module->periodes as $periodeid => $periode) {
	  ?>
        <fieldset>
    	  <legend>Horaires <?php echo $periode; ?></legend>

    	  <div class="horaires" id="horaires_<?php echo $periodeid; ?>">
		    <?php
		    foreach ($model->horaires as $index => $horaire):
			  if ($horaire->periodeid == $periodeid) {
				$this->renderPartial('/horaire/_form', array(
				    'model' => $horaire,
				    'index' => $index,
				    'periodeid' => $periodeid,
				));
			  }
		    endforeach;
		    $index++;
		    ?>
    	  </div>
    	  <div title="Aouter un horaire" class="addHoraireBtn add" id="addHoraireBtn_<?php echo $periodeid; ?>"/>
        </fieldset>
	  <?php
    }

    ?>


    <div class="row">
	  <?php echo $form->labelEx($model, 'nom'); ?>
	  <?php echo $form->textField($model, 'nom', array('size' => 45, 'maxlength' => 45)); ?>
	  <?php echo $form->error($model, 'nom'); ?>
    </div>


    <div class="row buttons">
	  <?php echo CHtml::submitButton('Enregistrer'); ?>
    </div>

    <?php $this->endWidget(); ?>

</div><!-- form -->

<?php
Yii::app()->domoHelper->loadJquery();


Yii::app()->clientScript->registerScript('manageHoraire', '
var _index =  ' . $index . ';
$(".addHoraireBtn").click(function(e){
    e.preventDefault();
    var _periodeid = this.id.replace("addHoraireBtn_", "");
    var _url = "' . Yii::app()->controller->createUrl("addHoraireByAjax", array("load_for" => $this->action->id)) . '?index="+_index+"&periodeid="+_periodeid;
     $.ajax({
        url: _url,
        success:function(response){
            $("#horaires_"+_periodeid).append(response);
            $("#horaires_"+_periodeid+" .crow").last().css({display:"none"}).animate({
                height: "toggle"
            });
        }
    });
    _index++;
});


//$(".delHoraireBtn").on( "click", function(){
$(document).on( "click", ".delHoraireBtn", function(){
    var element=$(this).parent();

    /* animate div */
    $(element).animate(
    {
        height: "toggle"
    }, 500,

	  function() {
        /* remove div */
        $(element).remove();
    });
})
', CClientScript::POS_END);

?>